package com.weishi.project.coach.controller;

import com.weishi.common.utils.DateUtils;
import com.weishi.common.utils.GPSUtil;
import com.weishi.common.utils.StringUtils;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.project.speedometer.service.IMantianxingService;
import com.weishi.project.speedometer.vo.PropellerFrequencyData;
import com.weishi.project.training.domain.ShipSpeedRecordLog;
import com.weishi.project.training.domain.TrainingGroupChannel;
import com.weishi.project.training.domain.WitAccLog;
import com.weishi.project.training.service.IAccelerationLogService;
import com.weishi.project.training.service.IShipSpeedRecordLogService;
import com.weishi.project.training.service.ITrainingGroupChannelService;
import com.weishi.project.training.service.IWitAccLogService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 教练员端 训练数据
 * zoulj 2021-07-27
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/coach/traningData")
public class TraningDataController {

    @Autowired
    private ITrainingGroupChannelService trainingGroupChannelService;

    @Autowired
    private IShipSpeedRecordLogService shipSpeedRecordLogService;

    @Autowired
    private IMantianxingService mantianxingService;

    @Autowired
    private IAccelerationLogService accelerationLogService;

    @Autowired
    private IWitAccLogService witAccLogService;

    @ApiOperation(value = "获取航道的详细数据")
    @RequestMapping(method = RequestMethod.GET, value = "/getChannelBasicsData")
    @ResponseBody
    public AjaxResult getChannelBasicsData(@RequestParam Integer channelId) throws Exception {
        Map<String, Object> ret = new HashMap<>();

        TrainingGroupChannel trainingGroupChannel = trainingGroupChannelService.getById(channelId);
        if (trainingGroupChannel != null) {
            ShipSpeedRecordLog params = new ShipSpeedRecordLog();
            params.setChannelId(trainingGroupChannel.getId());
            List<ShipSpeedRecordLog> list = shipSpeedRecordLogService.selectShipSpeedRecordLogList(params);
            if (StringUtils.isNotEmpty(list)) {
                ret.put("realData", getTableData(list, trainingGroupChannel.getStime()));

                //桨频数据
                long fristTime = Long.parseLong(list.get(0).getTimp());

//                List<PropellerFrequencyData> propellerFrequencyDatas = mantianxingService.getPropellerFrequencyData(list);
                List<PropellerFrequencyData> propellerFrequencyDatas = witAccLogService.qryPropellerFrequencyDate(null, channelId, fristTime);
                if (StringUtils.isEmpty(propellerFrequencyDatas)) {
                    propellerFrequencyDatas = accelerationLogService.qryPropellerFrequencyDate(null, channelId, fristTime);
                }
                Map<String, Object> propellerFrequencyMap = new HashMap<>();
                List<String> xp = new ArrayList<>();
                List<Long> yp = new ArrayList<>();
                List<Double> yd = new ArrayList<>();
                for (PropellerFrequencyData propellerFrequencyData : propellerFrequencyDatas) {
                    String realTime = propellerFrequencyData.getRealTime();
                    xp.add(realTime);
                    yp.add(Math.round(propellerFrequencyData.getAveragePropeller()));
                    //每桨功效
                    long tt = Long.parseLong(propellerFrequencyData.getEndTime()) - Long.parseLong(propellerFrequencyData.getStartTime());
                    yd.add(500 / (propellerFrequencyData.getDistance() / tt));
                }
                propellerFrequencyMap.put("xpropellerFrequency", xp);
                propellerFrequencyMap.put("ypropellerFrequency", yp);
                propellerFrequencyMap.put("yDistance", yd);
                ret.put("propellerFrequency", propellerFrequencyMap);

            }
        }

        return AjaxResult.success(ret);
    }

    @ApiOperation(value = "获取多传传感去数据")
    @RequestMapping(method = RequestMethod.GET, value = "/getWitAccDataList")
    @ResponseBody
    public AjaxResult getWitAccDataList(@RequestParam Integer channelId) throws Exception {
        Map<String, Object> ret = new HashMap<>();
        TrainingGroupChannel trainingGroupChannel = trainingGroupChannelService.getById(channelId);
        long firstTime = trainingGroupChannel.getStime().getTime();
        if (trainingGroupChannel != null) {
            List<WitAccLog> list = witAccLogService.qryWitAccLogList(channelId);
            if (StringUtils.isNotEmpty(list)) {
                List<String> x = new ArrayList<>();
                long stime = trainingGroupChannel.getStime().getTime();
                long etime = trainingGroupChannel.getEtime()==null ? new Date().getTime() : trainingGroupChannel.getEtime().getTime();
                while (etime-stime>0){
                    String time = getRealTime(firstTime,stime);
                    x.add(time);
                    stime += 100;
                }


                Map<String, Object> wx = new HashMap<>();
                Map<String, Object> anglex = new HashMap<>();
                List<Double> a1 = new ArrayList<>();
                List<Double> a2 = new ArrayList<>();
                List<Double> a3 = new ArrayList<>();
                List<Double> a4 = new ArrayList<>();

                List<Double> w1 = new ArrayList<>();
                List<Double> w2 = new ArrayList<>();
                List<Double> w3 = new ArrayList<>();
                List<Double> w4 = new ArrayList<>();


                Map<String, WitAccLog> a1_map = new HashMap<>();
                Map<String, WitAccLog> a2_map = new HashMap<>();
                Map<String, WitAccLog> a3_map = new HashMap<>();
                Map<String, WitAccLog> a4_map = new HashMap<>();
                for (WitAccLog witAccLog : list) {
                    String mac = witAccLog.getDevId();
                    String time = getRealTime(firstTime,Long.parseLong(witAccLog.getTimp()));
                    if (mac.equals("D0:6A:E4:69:DF:7C")) {
                        a1_map.put(time, witAccLog);
                    }
                    if (mac.equals("D8:B7:22:BF:23:0B")) {
                        a2_map.put(time, witAccLog);
                    }
                    if (mac.equals("F2:86:17:93:C6:B5")) {
                        a3_map.put(time, witAccLog);
                    }
                    if (mac.equals("F6:05:D9:5D:EC:9C")) {
                        a4_map.put(time, witAccLog);
                    }
                }

                for(String t : x){
                    WitAccLog witAccLog1 = a1_map.get(t);
                    if(witAccLog1!=null){
                        a1.add(witAccLog1.getAnglex());
                        w1.add(witAccLog1.getWx());
                    }else{
                        a1.add(0d);
                        w1.add(0d);
                    }

                    WitAccLog witAccLog2 = a2_map.get(t);
                    if(witAccLog2!=null){
                        a2.add(witAccLog2.getAnglex());
                        w2.add(witAccLog2.getWx());
                    }else{
                        a2.add(0d);
                        w2.add(0d);
                    }

                    WitAccLog witAccLog3 = a3_map.get(t);
                    if(witAccLog3!=null){
                        a3.add(witAccLog3.getAnglex());
                        w3.add(witAccLog3.getWx());
                    }else{
                        a3.add(0d);
                        w3.add(0d);
                    }

                    WitAccLog witAccLog4 = a4_map.get(t);
                    if(witAccLog4!=null){
                        a4.add(witAccLog4.getAnglex());
                        w4.add(witAccLog4.getWx());
                    }else{
                        a4.add(0d);
                        w4.add(0d);
                    }
                }

            wx.put("x", x);
            wx.put("w1", w1);
            wx.put("w2", w2);
            wx.put("w3", w3);
            wx.put("w4", w4);
            ret.put("wx", wx);

            anglex.put("x", x);
            anglex.put("y1", a1);
            anglex.put("y2", a2);
            anglex.put("y3", a3);
            anglex.put("y4", a4);
            ret.put("anglex", anglex);

        }
    }

        return AjaxResult.success(ret);
}

    private Map<String, Object> getTableData(List<ShipSpeedRecordLog> list, Date startTime) {
        Map<String, Object> ret = new HashMap<>();
        List<String> x = new ArrayList<>();
        List<Double> ySpeed = new ArrayList<>();
        List<Float> yDistanceTotal = new ArrayList<>();
        List<Double> yDistance = new ArrayList<>();
        ShipSpeedRecordLog pre = list.get(0);
        float distanceTotal = 0.0f;
        for (ShipSpeedRecordLog shipSpeedRecordLog : list) {
            float clearance = (float) GPSUtil.GetDistance1(pre.getLatitude(), pre.getLongitude(), shipSpeedRecordLog.getLatitude(), shipSpeedRecordLog.getLongitude());
            distanceTotal += clearance;
            x.add(getRealTime(startTime, Long.parseLong(shipSpeedRecordLog.getTimp())));
            ySpeed.add(shipSpeedRecordLog.getSpeed());
            yDistanceTotal.add(distanceTotal);
            yDistance.add(shipSpeedRecordLog.getDistance());
            pre = shipSpeedRecordLog;
        }

        ret.put("x", x);
        ret.put("ySpeed", ySpeed);
        ret.put("yDistanceTotal", yDistanceTotal);
        ret.put("yDistance", yDistance);
        return ret;
    }

    private String getRealTime(Date startTime, long end) {
        long start = startTime.getTime();
        return DateUtils.getStringTime(end - start);

    }

    private String getRealTime(long start, long end) {
        long time = end - start;
        if(time<=0){
            return "0:0:0";
        }
        long S = Math.round(time/1000.0);
        long ss = S%60;
        long mm = (S/60)%60;
        long hh = (S/60/60)%24;
        return hh+":"+mm+":"+ss+"."+S;

    }
}
